<?php

function create_uuid($prefix = "dy_") {    //可以指定前缀
    $str = md5(uniqid(mt_rand(), true));
    $uuid = substr($str, 0, 8) . '-';
    $uuid .= substr($str, 8, 4) . '-';
    $uuid .= substr($str, 12, 4) . '-';
    $uuid .= substr($str, 16, 4) . '-';
    $uuid .= substr($str, 20, 12);
    return $prefix . $uuid;
}

function create_dir($path) {
    if (!file_exists($path)) {
        create_dir(dirname($path));
        mkdir($path, 0777);
    }
}

function verify_token($token) {
    $User = new \Common\Model\UserModel();
    $user = $User->where("token = '" . $token . "'")->find();
    $result['code'] = "200";
    if ($user == null) {
        $result['code'] = "407"; //无效的token
        echo json_encode($result, JSON_PRETTY_PRINT);
        die();
    }
    $user['password'] = "";
//    $result['result'] = $user;
//    echo json_encode($result, JSON_PRETTY_PRINT);
    return $user;
}

function verify_app_key($app_key) {
    if (APP_KEY != $app_key) {
        $result['code'] = "406"; //非法的App_key
//        echo json_encode($result, JSON_PRETTY_PRINT);
//        die();
    }
}

/*
  function verify_app_key($app_key){
  $user = new \Common\Model\UserModel();
  show_bug($user);
  }
 */

/**
 * arr2option
 * 数组转成<option></option>列表
 * @param array $arr 待转换的数组
 * @param string $value option选项中的value所对应的数组中的key
 * @param string $name option选项中<option>与</option>之间用于描述的文字对应数组的key
 * @param string $selected 与value对比，相同的值则设置为选中状态
 * @return string 返回html代码字符串
 * 参考：http://www.thinkphp.cn/topic/4136.html
 */
function arr2option($arr, $value, $name, $selected = "") {
    $option = "";
    foreach ($arr as $v) {
        if (!is_array($v))
            continue;
        if (!isset($v[$value]) || !isset($v[$name]))
            continue;
        if ($v[$value] == $selected) {
            $option.="<option value=\"{$v[$value]}\" selected>{$v[$name]}</option>" . PHP_EOL;
        } else {
            $option.="<option value=\"{$v[$value]}\">{$v[$name]}</option>" . PHP_EOL;
        }
    }
    return $option;
}
